fix(gaxios): prefer globalThis.fetch over import('node-fetch') on Node.js 18+#916
Closed
whongj wants to merge 2 commits intogoogleapis:mainfrom
Closed
fix(gaxios): prefer globalThis.fetch over import('node-fetch') on Node.js 18+#916whongj wants to merge 2 commits intogoogleapis:mainfrom
whongj wants to merge 2 commits intogoogleapis:mainfrom
Conversation
…e.js 18+
Node.js v22.22.0 crashes when gaxios tries `await import('node-fetch')`:
TypeError: Cannot convert undefined or null to object
at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:213:12)
This causes google-auth-library JWT exchange to fail, breaking all
Vertex AI usage (GoogleGenAI, googleapis/*) on Node.js v22+.
Root cause: `#getFetch()` checks for `window` (browser) but ignores
`globalThis.fetch`, available natively since Node.js 18.
Fix: check `typeof globalThis.fetch === 'function'` first.
Reported in: openclaw/openclaw#32245
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
sofisl
approved these changes
Mar 12, 2026
Contributor
|
Hi @whongj thank you for the contribution! In general this lgtm but tests are not passing. I'm in the middle of migrating this library to google-cloud-node so I will close this PR for now, would you mind reopening it in google-cloud-node once the migration is complete (should be by next week). Then we can work on the tests. Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Node.js v22.22.0 crashes when gaxios executes
await import("node-fetch")from a CJS context:This kills
google-auth-libraryJWT token exchange, breaking all Vertex AI calls (@google/genai,googleapis/*) on Node.js v22+.Root Cause
In
#getFetch(), the code checks forwindow(browser detection) but never checksglobalThis.fetch, which has been available natively since Node.js 18:Fix
Check
globalThis.fetchbefore falling back tonode-fetch. One line:Verification
Also verified:
google-auth-libraryJWT.getAccessToken()succeeds after patch on Node.js v22.22.0.Related